var onlyOpenTitle = "首页";
var opentabs = 8; //允许打开的TAB数量
var menus;
$(function () {
	$.post(getRootPath()+"/json/initNav.action",'',function(jsondata){
			menus=jsondata;  
		    initNav();
		    tabClose();
		},'json');

    //Tab右键菜单
    $('#closeMenu').menu({
        onClick: function (item) {
            closeTab(item.id);
        }
    });

});
function initNav() {
	AccordionTree.init();
	 $('#tabs').tabs({
	        tools: [{
	            iconCls: 'icon-reload',
	            handler: function() {
	                var tab = $('#tabs').tabs('getSelected');
	                if (tab.panel('options').title != onlyOpenTitle){
	                    closeTab('refresh');
	                }else{
	                	//document.frames("syzszwl").location.reload(true);
	                	var myiframe = document.getElementById("syzszwl");

	                	myiframe.src = myiframe.src; //这样同样可以起到刷新的效果。
	                }
	                return false;
	            }
	        }, {
	            iconCls: 'icon-cancel',
	            handler: function() {
	                if (confirm('亲，确认要关闭所有窗口吗？')) {
	                    closeTab("closeall");
	                }
	            }
	        }],
	        onContextMenu: function(e, title) {
	            e.preventDefault();
	            $('#closeMenu').menu('show', {
	                left: e.pageX,
	                top: e.pageY
	            });
	            $('#tabs').tabs('select', title);
	        }
	    });
}
//手风琴 +tree
var AccordionTree = {
    init:function () {
		 $("#wnav").accordion({
	         fit: true, border: false, onAdd: function (title, index) {
			     if(index>0){
	             $('#nt' + (index-1)).tree({
	                 lines: false,
	                 animate: true,
	                 data: menus[index-1].children,
	                 onClick: function(node) {
	                     if (node.attributes.url != "" && node.attributes.url != '#') {
	                    	 console.log(node.iconCls);
	                         addTab(node.text, node.attributes.url + '?navid=' + node.id, node.iconCls);
	                     } else {
	                         $('#nt' + (index-1)).tree('toggle', node.target);
	                     }
	                 }
	             });
			     
	         }
		 }
	     });
	     $.each(menus, function (i, n) {
	         $('#wnav').accordion('add', {
	             title: n.text,
	             content: '<div style="padding-top:5px;"><ul id="nt' + i + '">'+i+'</ul></div>',
	             border: false,
	             iconCls: n.iconCls,
	             selected:true
	         });
	     });
	     $('#wnav').accordion('add', {title:'zliang'});
	     $('#wnav').accordion('remove','zliang');
    }
};
function addTab(subtitle, url, icon) {
	console.log(icon);
    if (url == "" || url == "#")
        return false;
    var tabCount = $('#tabs').tabs('tabs').length;
    var hasTab = $('#tabs').tabs('exists', subtitle);
    var add = function () {
        if (!hasTab) {
            $('#tabs').tabs('add', {
                title: subtitle,
                content: createFrame(url),
                closable: true,
                icon: icon
            });
        } else {
            $('#tabs').tabs('select', subtitle);
            //closeTab('refresh'); //选择TAB时刷新页面
        }
    };
    if (tabCount > opentabs && !hasTab) {
        var msg = '<b>您当前打开了太多的页面，如果继续打开，会造成程序运行缓慢，无法流畅操作！</b>';
        $.messager.confirm("系统提示", msg, function (b) {
            if (b) add();
            else return false;
        });
    } else {
        add();
    }
}

function createFrame(url) {
    var s = '<iframe scrolling="auto" frameborder="0"  style="width:100%;height:100%;" src="' + url + '" ></iframe>';
    return s;
}

function tabClose() {
    /*双击关闭TAB选项卡*/
    $(".tabs-inner").live('dblclick', function () {
        var subtitle = $(this).children(".tabs-closable").text();
        if (subtitle != onlyOpenTitle && subtitle != "")
            $('#tabs').tabs('close', subtitle);
    });
}

function closeTab(action) {
    var alltabs = $('#tabs').tabs('tabs');
    var currentTab = $('#tabs').tabs('getSelected');
    var allTabtitle = [];
    $.each(alltabs, function (i, n) {
        allTabtitle.push($(n).panel('options').title);
    });
    switch (action) {
        case "refresh":
            var iframe = $(currentTab.panel('options').content);
            var src = iframe.attr('src');
            $('#tabs').tabs('update', {
                tab: currentTab,
                options: {
                    content: createFrame(src)
                }
            });
            break;
        case "close":
            var currtab_title = currentTab.panel('options').title;
            $('#tabs').tabs('close', currtab_title);
            break;
        case "closeall":
            $.each(allTabtitle, function (i, n) {
            	if(i>0){
                    $('#tabs').tabs('close', n);
            	}
            });
            break;
        case "closeother":
            var currtab_title = currentTab.panel('options').title;
            $.each(allTabtitle, function (i, n) {
                if (n != currtab_title && n != onlyOpenTitle) {
                    $('#tabs').tabs('close', n);
                }
            });
            $('#tabs').tabs('open', onlyOpenTitle);
            break;
        case "closeright":
            var tabIndex = $('#tabs').tabs('getTabIndex', currentTab);

            if (tabIndex == alltabs.length - 1) {
                alert('亲，后边没有啦 ^@^!!');
                return false;
            }
            $.each(allTabtitle, function (i, n) {
                if (i > tabIndex) {
                    if (n != onlyOpenTitle) {
                        $('#tabs').tabs('close', n);
                    }
                }
            });

            break;
        case "closeleft":
            var tabIndex = $('#tabs').tabs('getTabIndex', currentTab);
            if (tabIndex == 1) {
                alert('亲，前边那个上头有人，咱惹不起哦。 ^@^!!');
                return false;
            }
            $.each(allTabtitle, function (i, n) {
                if (i < tabIndex) {
                    if (n != onlyOpenTitle) {
                        $('#tabs').tabs('close', n);
                    }
                }
            });

            break;
        case "exit":
            $('#closeMenu').menu('hide');
            break;
    }
}

function getRootPath(){  
    //获取当前网址，如： http://localhost:8088/test/test.jsp  
    var curPath=window.document.location.href;  
    //获取主机地址之后的目录，如： test/test.jsp  
    var pathName=window.document.location.pathname;  
    var pos=curPath.indexOf(pathName);  
    //获取主机地址，如： http://localhost:8088  
    var localhostPaht=curPath.substring(0,pos);  
    //获取带"/"的项目名，如：/test  
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);  
    return(localhostPaht+projectName);  
}  